﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace BlogDH
{
    public partial class List : System.Web.UI.Page
    {
        protected string AreaName = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GeBlogList();
            }
        }

        private void GeBlogList()
        {
            int pageSize = CommValue.PageSize;
            UrlParams param = new UrlParams();
            StringBuilder sqlBuilder = new StringBuilder();

            int pageid = 1;
            if (!string.IsNullOrEmpty(Request.QueryString["pageid"]))
            {
                string pageidStr = Request.QueryString["pageid"].ToString();
                if (!int.TryParse(pageidStr, out pageid) && pageid == 0)
                    pageid = 1;
            }

            string areaEname = Request.QueryString["area"];
            areaEname = areaEname == null ? string.Empty : areaEname;

            //省份列表
            List<Model.Area> areas = CommValue.AreaList;

            //省份对应的AreaID
            Model.Area area = areas.FirstOrDefault(x => x.Ename == areaEname);
            if (area != null && area.AreaID > 0)
            {
                sqlBuilder.AppendFormat("sheng={0}", area.AreaID);
                AreaName = area.Name;
            }
            else
            {
                sqlBuilder.Append("sheng=-1");
            }

            //设置地区检索条件
            this.Literal1.Text = CommMethod.GetAreaHtml(areaEname);

            //URL分页条件
            param.Area = areaEname;

            //读取数据库
            BLL.BlogInfo bll = new BLL.BlogInfo();
            int totalCount = bll.GetRecordCount(sqlBuilder.ToString());
            if (totalCount > 0)
            {
                System.Data.DataSet ds = bll.GetPageList(pageSize, pageid, totalCount, sqlBuilder.ToString());

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.Repeater1.DataSource = ds;
                    this.Repeater1.DataBind();
                }


                if (totalCount > pageSize)
                {
                    int totalPageCount = totalCount / pageSize;
                    if (totalCount % pageSize > 0)
                        totalPageCount++;
                    this.ltr_paging.Text = CommMethod.GetPaging(param, totalPageCount, pageid);
                }
            }
        }

    }
}